package cqie.personal.frame.controller;

import cqie.personal.frame.bo.*;
import cqie.personal.frame.config.RequiredPermission;
import cqie.personal.frame.entity.UserInfo;
import cqie.personal.frame.enums.Code;
import cqie.personal.frame.exception.DataBaseUpdateException;
import cqie.personal.frame.exception.ValidationFailException;
import cqie.personal.frame.service.*;
import cqie.personal.frame.service.impl.CharacterServiceImpl;
import cqie.personal.frame.util.JwtUtil;
import cqie.personal.frame.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yeeeeee
 * @since 2021-09-01
 */
@RestController
@Api(tags = "管理员功能模块")
@RequestMapping("/user-admin")
@RequiredPermission(2)
public class AdminController {

    @Resource
    private MenuInfoService menuInfoService;
    @Resource
    private CharacterServiceImpl characterServiceImpl;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private DepartmentInfoService departmentInfoService;
    @Resource
    private ClassInfoService classInfoService;
    @Resource
    private CourseInfoService courseInfoService;

    @ApiOperation("新增菜单")
    @PostMapping("/addMenu")
    public ResultVO addMenu(@RequestBody AddModifyMenuBO menuBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,menuBO.getUid())){
            if(menuInfoService.addMenu(menuBO)){
                return new ResultVO();

            }else {
                return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
            }
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("编辑菜单")
    @PostMapping("/modifyMenu")
    public ResultVO modifyMenu(@RequestPart("addModifyMenuBO") AddModifyMenuBO addModifyMenuBO , @RequestParam(value = "file",required = false) MultipartFile file , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyMenuBO.getUid())){
            if(menuInfoService.modifyMenu(addModifyMenuBO , file)){
                return new ResultVO();
            }else {
                return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
            }
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("添加角色")
    @PostMapping("/addCharacter")
    public ResultVO addRole(@RequestBody AddModifyCharacterBO addModifyCharacterBO , HttpServletRequest request) throws ValidationFailException, DataBaseUpdateException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyCharacterBO.getUid())) {
            if(characterServiceImpl.addCharacter(addModifyCharacterBO)){
                return new ResultVO();
            }else {
                return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
            }
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("编辑角色")
    @PostMapping("/modifyCharacter")
    public ResultVO modifyRole(@RequestBody AddModifyCharacterBO addModifyCharacterBO , HttpServletRequest request) throws DataBaseUpdateException, ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyCharacterBO.getUid())) {
            if(characterServiceImpl.modifyCharacter(addModifyCharacterBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("管理员新增用户")
    @PostMapping("/addUser")
    public ResultVO addUser(@RequestBody AddModifyUserBO userBO  , HttpServletRequest request) throws ValidationFailException, IOException, DataBaseUpdateException {
        String token = request.getHeader("token");
        if(userTokenValid(token,userBO.getUid())) {
            if(userInfoService.addUser(userBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }


    @ApiOperation("编辑用户信息")
    @PostMapping("/modifyUser")
    public ResultVO modifyUser(@RequestBody AddModifyUserBO userBO, HttpServletRequest request) throws ValidationFailException, IOException, DataBaseUpdateException {
        String token = request.getHeader("token");
        if(userTokenValid(token,userBO.getUid())) {
            if(userInfoService.modifyUser(userBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("新增部门")
    @PostMapping("/addDepartment")
    public ResultVO addDepartment(@RequestBody AddModifyDepartmentBO addModifyDepartmentBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyDepartmentBO.getUid())){
            if(departmentInfoService.addDepartment(addModifyDepartmentBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("修改部门")
    @PostMapping("/modifyDepartment")
    public ResultVO modifyDepartment(@RequestBody AddModifyDepartmentBO addModifyDepartmentBO , HttpServletRequest request) throws ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyDepartmentBO.getUid())){
            if(departmentInfoService.modifyDepartment(addModifyDepartmentBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Add new Class")
    @PostMapping("/addClass")
    public ResultVO addClass(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(classInfoService.addClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Modify Class")
    @PostMapping("/modifyClass")
    public ResultVO modifyClass(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(classInfoService.modifyClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("del Class")
    @PostMapping("/delClass")
    public ResultVO delClass(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws ValidationFailException, DataBaseUpdateException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(classInfoService.delClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Add new Course")
    @PostMapping("/addCourse")
    public ResultVO addCourse(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(courseInfoService.addCourse(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Modify course")
    @PostMapping("/modifyCourse")
    public ResultVO modifyCourse(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws ValidationFailException {
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(courseInfoService.modifyCourse(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Modify course table")
    @PostMapping("/modifyCourseTable")
    public  ResultVO modifyCourseTable(@RequestBody ModifyCourseBO modifyCourseBO , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,modifyCourseBO.getOperator())){
            if(courseInfoService.modifyCourseTable(modifyCourseBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation("Add new student to class")
    @PostMapping("/addStudentIntoClass")
    public  ResultVO addStudentIntoClass(@RequestBody AddModifyStudentBO addModifyBO , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(userInfoService.addStudentIntoClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation(" remove Student From Class")
    @PostMapping("/removeStudentFromClass")
    public  ResultVO  removeStudentFromClass(@RequestBody AddModifyStudentBO addModifyBO , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(userInfoService.removeStudentFromClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation(" add New Course Into Class")
    @PostMapping("/addNewCourseIntoClass")
    public  ResultVO  addNewCourseIntoClass(@RequestBody AddModifyCourseClassBO addModifyBO , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(classInfoService.addNewCourseIntoClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    @ApiOperation(" Remove course form class")
    @PostMapping("/removeCourseFormClass")
    public  ResultVO  removeCourseFormClass(@RequestBody AddModifyBO addModifyBO , HttpServletRequest request) throws  ValidationFailException{
        String token = request.getHeader("token");
        if(userTokenValid(token,addModifyBO.getOperator())){
            if(classInfoService.removeCourseFormClass(addModifyBO)){
                return new ResultVO();
            }
            return new ResultVO(Code.OPERATION_FAIL.getCode(),Code.OPERATION_FAIL.getMessage());
        }
        return new ResultVO(Code.ACCESS_DENIED.getCode(),"权限不匹配,拒绝访问");
    }

    private boolean userTokenValid(String token , Integer uid){
        UserInfo user = JwtUtil.parsingToken(token);
        return user.getUid().equals(uid);
    }



}
